<template>
  <div ref="custom-table" class="custom-table-container">
    <vab-query-form>
      <vab-query-form-top-panel>
        <el-form
          ref="form"
          :inline="true"
          :model="queryForm"
          @submit.native.prevent
        >
          <el-form-item label="账号" label-width="50px">
            <el-input v-model="queryForm.accountNum" placeholder="账号" />
          </el-form-item>
          <el-form-item label="开始时间" label-width="100px">
            <el-date-picker
              v-model="queryForm.beginDate"
              placeholder="开始时间"
              style="width: 180px"
              type="date"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
          <el-form-item label="结束时间" label-width="100px">
            <el-date-picker
              v-model="queryForm.endDate"
              placeholder="结束时间"
              style="width: 180px"
              type="date"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              icon="el-icon-search"
              native-type="submit"
              type="primary"
              @click="handleQuery"
            >
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-top-panel>
    </vab-query-form>

    <el-table
      ref="tableSort"
      v-loading="listLoading"
      :border="true"
      :data="list"
      :height="height"
      :size="lineHeight"
      :stripe="true"
      @selection-change="setSelectRows"
    >
      <el-table-column
        align="center"
        label="序号"
        show-overflow-tooltip
        width="95"
      >
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        v-for="(item, index) in finallyColumns"
        :key="index"
        align="center"
        :label="item.label"
        :prop="item.prop"
        :sortable="item.sortable"
        :width="item.width"
      >
        <template #default="{ row }">
          {{ row[item.prop] }}
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageIndex"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
  import { QueryLoginLogPageList } from '@/api/sysSetting/loginLog.js'
  import { getNowDate } from '@/utils/myjs.js'

  export default {
    name: 'CustomTable',
    data() {
      return {
        height: this.$baseTableHeight(1),
        lineHeight: 'mini',
        checkList: ['账号', '登录IP', '登录时间'],
        columns: [
          {
            label: '账号',
            width: '200',
            prop: 'accountNum',
          },
          {
            label: '登录IP',
            width: '200',
            prop: 'loginIP',
          },
          {
            label: '登录时间',
            width: '200',
            prop: 'createTime',
          },
        ],
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageIndex: 1,
          pageSize: 20,
          accountNum: '',
          beginDate: getNowDate(),
          endDate: getNowDate(),
        },
      }
    },
    computed: {
      finallyColumns() {
        return this.columns.filter((item) =>
          this.checkList.includes(item.label)
        )
      },
    },
    created() {
      this.fetchData()
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageIndex = val
        this.fetchData()
      },
      handleQuery() {
        this.queryForm.pageIndex = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const {
          data: { list, total },
        } = await QueryLoginLogPageList(this.queryForm)
        this.list = list
        this.total = total
        this.listLoading = false
      },
    },
  }
</script>

<style lang="scss" scoped>
  .custom-table-container {
    ::v-deep {
      i {
        cursor: pointer;
      }
    }
  }
</style>
